import { ref, reactive } from 'vue';

/** 封装请求方法，同步loading状态出去
 * 使用方法
const { fetch, loading: loading2 } = useLoadingFetch(BasEnumApi.getByType);
var res = await fetch(paramObject);
 */
export const useLoadingFetch = (fn: Function) => {
	const loading = ref(false);
	const result = ref();

	const fetch = async (paramJson: any = undefined) => {
		loading.value = true;
		try {
			if (paramJson == undefined) {
				result.value = await fn();
			} else {
				result.value = await fn(paramJson);
			}
			return result;
		} finally {
			loading.value = false;
		}
	};

	return { fetch, loading, result };
};
